
var Comments = (function(){

	function handleElements() {
		
	}

	function handleGrid() {

		var boolRender = function(v, r, i) {
			if(v == '1'){
				return '<i class="icon-thumbs-up text-success"></i>';
			}else if(v == '2'){
				return '<i class="icon-thumbs-down text-danger"></i>';
			}else{
				return '?';	
			}
		};

		var renderDate = function (v, r, i) {
			return moment(v).format('dddd, MMMM Do YYYY');
		};

		$('#grid').datagrid({
			url: siteURL('comments/com_events/load_data'),
			idField:'ce_id',
			treeField:'e_name',
			title: 'Comments of Events',
			height: 400,
			rownumbers: true,
			pagination: true,
			pagePosition: 'top',
			autoRowHeight: false,
			checkbox: true,
			singleSelect: false,
			columns: [[
				{field: 'ck', checkbox: true},
				{field: 'e_name', title: 'Event', width: 200, sortable: true},
				{field: 'ce_email', title: 'email', width: 200, sortable: true},
				{field: 'ce_fullname', title: 'Fullname', width: 200, sortable: true},
				{field: 'ce_comments', title: 'Comments', width: 250, align: 'center', sortable: true},
				{field: 'ce_created_date', title: 'created at', width: 100, align: 'center', sortable: true,formatter: renderDate},
				{field: 'ce_approved_date', title: 'approved at', width: 100, align: 'center', sortable: true,formatter: renderDate},
				{field: 'ce_approved', title: 'Approved ?', width: 100, align: 'center', formatter: boolRender, sortable: true}
			]]
		});

		var pager = $('#grid').datagrid('getPager');
		pager.pagination({
			buttons: [
				{text: '<i class="icon-thumbs-up"></i> Approve',  handler: approvedRecord},
				{text: '<i class="icon-thumbs-down"></i> Reject',  handler: rejectRecord}
			]
		});
	}

	function handleWindow() {
		$('#win').show();
		$('#win').dialog({
			modal: true,
			width: 420,
			title: 'Comments Editor',
			buttons: [
				{
					text: 'Submit',
					handler: function() {
						$('#form').form('submit', {
							url: siteURL('articles/categories/save_data'),
							success: function() {
								$('#win').dialog('close');
								$('#grid').datagrid('reload');
							}
						});
					}
				},
				{
					text: 'Close',
					handler: function() {
						$('#win').dialog('close');
					}
				}
			]
		});
		$('#win').dialog('close');
	}


	function handleSearch() {
		$('#omnisearch').on('keypress', function(e){
			if (e.keyCode === 13) {
				var query = $(this).val();
				$('#grid').datagrid('load', {
					query: query
				});
			}
		});
	}

	function approvedRecord() {
		var record = $('#grid').datagrid('getSelected');
		if ( ! record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}

		$.messager.confirm('Confirm', 'Approve selected record?', function(r){
			if (r) {
				$.ajax({
					url: siteURL('comments/com_events/approve'),
					type: 'post',
					dataType: 'json',
					data: record
				})
				.done(function(){
					$('#grid').datagrid('reload');
				});	
			}
		});
	}

	function rejectRecord() {
		var record = $('#grid').datagrid('getSelected');
		if ( ! record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}

		$.messager.confirm('Confirm', 'Reject selected record?', function(r){
			if (r) {
				$.ajax({
					url: siteURL('comments/com_events/reject'),
					type: 'post',
					dataType: 'json',
					data: record
				})
				.done(function(){
					$('#grid').datagrid('reload');
				});	
			}
		});
	}

	return {
		init: function() {
			handleElements();
			handleGrid();
			handleWindow();
			handleSearch();
		}
	};

}(jQuery));

$(document).ready(Comments.init);